package com.ruoyi.server.api.imguser.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.server.api.imgabout.pojo.About;
import com.ruoyi.server.api.imgabout.service.AboutService;
import com.ruoyi.server.api.imguser.mapper.ImgUserMapper;
import com.ruoyi.server.api.imguser.pojo.ImgUser;
import com.ruoyi.server.api.imguser.service.ImgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName UserviceImpl
 * @Description TODO
 * @Author wsb
 * @Date 2024/10/22 8:42
 **/
@Service
public class ImgUserServiceImpl implements ImgUserService {
    @Resource
    ImgUserMapper usersMapper;
    @Autowired
    AboutService aboutService;

    /**
     * @Author
     * @Description 查询用户
     * @Date 2024/11/15 10:32
     * @Return * @param null
     **/

    @Override
    public ImgUser getImgUserById(String userId) {
        ImgUser imgUsers = usersMapper.selectUserById(userId);
        About about = aboutService.selectImgAboutById(1);
        if(StringUtils.isNotNull(imgUsers)&&StringUtils.isNotEmpty(imgUsers.getPhone())&&StringUtils.isNotEmpty(about.getPhone())){
            imgUsers.setMerchant(imgUsers.getPhone().equals(about.getPhone()));
        }
        return imgUsers;
    }


    /**
     * @Author wsb
     * @Description 插入用户
     * @Date 2024/11/15 10:33
     * @Return * @param user
     **/
    @Override
    public int insertImgUser(ImgUser user) {
        return usersMapper.insertImgUser(user);
    }

    @Override
    public ImgUser updatePhoneByUserId(ImgUser user) {
        usersMapper.updateUserByUserId(user);
        About about = aboutService.selectImgAboutById(1);
        if(StringUtils.isEmpty(user.getPhone())&&StringUtils.isEmpty(about.getPhone())){
            user.setMerchant(user.getPhone() == about.getPhone());
        }
        return user;
    }

    /**
     * @param phone
     * @Author Lx
     * @Description 判断手机号是否已经存在
     * @Date 2024/11/19 9:04
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @Override
    public ImgUser getPhone(String phone) {
        return usersMapper.getPhone(phone);
    }

    /**
    * @Author 帅
    * @Description 使用手机号查询头像地址
    * @Date 2024/11/22 14:17
    * @Return 头像地址
    * @param userId 用户手机号
    */
    @Override
    public String getUserAvatarUrl(String userId) {
        return usersMapper.getUserAvatarUrl(userId);
    }

    @Override
    public boolean checkUserIsMerchant(String userId) {
        ImgUser imgUsers = usersMapper.selectUserById(userId);
        About about = aboutService.selectImgAboutById(1);
        return imgUsers.getPhone() == about.getPhone();
    }
}
